package com.bobi.tfca.controller.activity;

import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.model.vo.ActivityVO;
import com.bobi.tfca.model.vo.ArticleVO;
import com.bobi.tfca.model.vo.MemberVO;
import com.bobi.tfca.service.ActivityService;
import com.bobi.tfca.web.annotation.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李文发
 * @date 2024/4/15
 */
@RestController
@RequestMapping("/activity")
@Api(tags = "活动接口")
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @ApiOperation(value = "获得全部活动")
    @GetMapping("/findAll")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户")})
    public R<List<ActivityVO>> findAll(@CurrentUser MemberEntity member) {
        List<ActivityVO> list = activityService.findAll(member);
        return R.data(list);
    }

    @ApiOperation(value = "查询所有活动成员")
    @GetMapping("/findAllMembers/{activityId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "activityId", value = "活动id")})
    public R<List<MemberVO>> findAllMembers(@PathVariable("activityId") Integer activityId) {
        List<MemberVO> list = activityService.findAllMembers(activityId);
        return R.data(list);
    }

    @ApiOperation(value = "查询活动细节内容")
    @GetMapping("/getActivityDetails/{activityId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "activityId", value = "活动id")})
    public R<ActivityVO> getActivityDetails(@CurrentUser MemberEntity member, @PathVariable("activityId") Integer activityId) {
        ActivityVO activityVO = activityService.getActivityDetails(member, activityId);
        return R.data(activityVO);
    }

    @ApiOperation(value = "组织活动")
    @PostMapping("/organizeActivity")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(value = "activityVO", name = "活动")})
    public R<Integer> organizeActivity(@CurrentUser MemberEntity member, @RequestBody ActivityVO activityVO) {
        Integer activityId = activityService.organizeActivity(member, activityVO);
        return R.data(activityId);
    }

    @ApiOperation(value = "移除活动成员")
    @PostMapping("/removeMember/{activityId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "memberIds", value = "会员id列表")})
    public R<String> removeMember(@PathVariable("activityId") Integer activityId, @CurrentUser MemberEntity member, @RequestBody List<Integer> memberIds) {
        Integer memberId = member.getMemberId();
        if (memberIds.contains(memberId)) {
            return R.failed("不能移除自己");
        }
        // TODO 角色权限校验
        Boolean success = activityService.removeMember(activityId, memberIds);
        return success ? R.data("移除成功") : R.failed("移除失败，活动已经结束或取消");
    }   

    @ApiOperation(value = "取消报名")
    @PostMapping("/cancelSign/{activityId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "activityId", value = "活动id")})
    public R<String> cancelSign(@CurrentUser MemberEntity member, @PathVariable("activityId") Integer activityId) {
        Boolean success = activityService.cancelSign(member, activityId);
        return success ? R.data("取消成功") : R.failed("取消失败，活动已经开始");
    }

    @ApiOperation(value = "报名")
    @PostMapping("/signUp/{activityId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "activityId", value = "活动id")})
    public R<String> signUp(@CurrentUser MemberEntity member, @PathVariable("activityId") Integer activityId) {
        Integer res = activityService.signUp(member, activityId);
        switch (res) {
            case 0: return R.success("报名成功");
            case 1: return R.failed("当前活动状态不可报名");
            case 2: return R.failed("不能重复报名");
            case 3: return R.failed("报名人数已达上限");
            default: return R.failed("报名失败");
        }
    }

    @ApiOperation(value = "结束活动")
    @PostMapping("/finishActivity/{activityId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "activityId", value = "活动id")})
    public R<String> finishActivity(@CurrentUser MemberEntity member, @PathVariable("activityId") Integer activityId) {
        Boolean success = activityService.finishActivity(activityId);
        return success ? R.data("结束活动成功") : R.failed("结束活动失败，活动未开始或已经取消或结束");
    }

    @ApiOperation(value = "取消活动")
    @PostMapping("/cancelActivity/{activityId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "activityId", value = "活动id")})
    public R<String> cancelActivity(@CurrentUser MemberEntity member, @PathVariable("activityId") Integer activityId) {
        Boolean success = activityService.cancelActivity(activityId);
        return success ? R.data("取消活动成功") : R.failed("取消活动失败，活动已经结束");
    }

    @ApiOperation(value = "获得我的全部活动")
    @GetMapping("/findMyActivities")
    @ApiImplicitParams({@ApiImplicitParam(name = "member", value = "当前用户")})
    public R<List<ActivityVO>> findMyActivities(@CurrentUser MemberEntity member) {
        List<ActivityVO> list = activityService.findMyActivities(member);
        return R.data(list);
    }
}
